package com.shop2cn.iapi.openapibiz.testcase;

import java.text.SimpleDateFormat;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.openapibiz.parameter.Application;
import com.shop2cn.iapi.openapibiz.parameter.ListAuthorizesBean;
import com.shop2cn.iapi.openapibiz.service.ListAuthorizesCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.database.model.AuthCode;
import com.ymttest.database.sqlwapper.OpenApiWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
/**
*查询所有授权
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_ListAuthorizes{
	private static ListAuthorizesBean listauthorizesBean;
	private static ListAuthorizesCall listauthorizesCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询所有授权");
	}

	@Before
	public void caseUp() {
		listauthorizesBean=new ListAuthorizesBean();
		listauthorizesCall=new ListAuthorizesCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(listauthorizesCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ListAuthorizes_001() {
		Logger.start(true,"查询所有授权");
		try {
			long sellerId = Long.valueOf(EnvSetup.getData("openapi_seelerid"));
			
			listauthorizesBean.setSellerId(sellerId);
			listauthorizesCall.setData(listauthorizesBean);
			listauthorizesCall.callService();			
			
			Logger.verifyEquals("200", listauthorizesCall.getCode(), "验证Code");
			
			List<Application> applicaton = listauthorizesCall.getApplication().getContent();
			OpenApiWapper openApiWapper = new OpenApiWapper();
			List<com.ymttest.database.model.Application> applications = openApiWapper.selectAuthorizeApplicationBySellerId((long)listauthorizesBean.getSellerId());
			
			SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
			Logger.verifyEquals(applications.size(), applicaton.size(), "验证返回数据量");

			for(int i=0;i<applicaton.size();i++)
			{
				AuthCode authCode = openApiWapper.selectAuthCodeByAppId(applicaton.get(i).getAppId()).get(0);
				
				Logger.verifyEquals(time.format(authCode.getCreate_time()), applicaton.get(i).getCreateTime(), "验证返回createTime与数据库一致");
				Logger.verifyEquals(time.format(authCode.getExpire_time()), applicaton.get(i).getExpireTime(), "验证返回ExpireTime与数据库一致");
				Logger.verifyEquals(authCode.getSeller_id(), applicaton.get(i).getSellerId(), "验证返回SellerId与数据库一致");
				Logger.verifyEquals(authCode.getAuth_code(), applicaton.get(i).getAuthCode(), "验证返回AuthCode与数据库一致");
				Logger.verifyEquals(applications.get(i).getApp_id(), applicaton.get(i).getAppId(), "验证返回appId与数据库一致");
				Logger.verifyEquals(applications.get(i).getApp_name(), applicaton.get(i).getAppName(), "验证返回appName与数据库一致");
				Logger.verifyEquals(applications.get(i).getType(), applicaton.get(i).getType(), "验证返回type与数据库一致");

				if(applications.get(i).getType().equals("USER_DEV"))
				{
//					Logger.verifyEquals(applications.get(i).getApp_secret(), applicaton.get(i).getAppSecret(), "验证返回AppSecret与数据库一致");

				}
				else {
					Logger.verifyEquals(null, applicaton.get(i).getAppSecret(), "验证返回AppSecret与数据库一致");

				}
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ListAuthorizes_002() {
		Logger.start(true,"查询所有授权-sellerid不传");
		try {
			
			listauthorizesCall.setData(listauthorizesBean);
			listauthorizesCall.callService();			
			
			Logger.verifyEquals("400", listauthorizesCall.getCode(), "验证返回AppSecret与数据库一致");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ListAuthorizes_003() {
		Logger.start(true,"查询所有授权-sellerid传0");
		try {
			listauthorizesBean.setSellerId((long)0);
			listauthorizesCall.setData(listauthorizesBean);
			listauthorizesCall.callService();			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ListAuthorizes_004() {
		Logger.start(true,"查询所有授权-sellerid传不存在");
		try {
			listauthorizesBean.setSellerId((long)TestDataManager.getRandomIntNum(6));
			listauthorizesCall.setData(listauthorizesBean);
			listauthorizesCall.callService();			
			
			Logger.verifyEquals("200", listauthorizesCall.getCode(), "验证返回code");
			Logger.verifyEquals(null, listauthorizesCall.getApplication().getContent(), "验证返回内容空");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ListAuthorizes_005() {
		Logger.start(true,"神器用户查询不到信息");
		try {
			long sellerId = 500004881;
			
			listauthorizesBean.setSellerId(sellerId);
			listauthorizesCall.setData(listauthorizesBean);
			listauthorizesCall.callService();			

			Logger.verifyEquals("200", listauthorizesCall.getCode(), "验证返回code");
			Logger.verifyEquals(0, listauthorizesCall.getApplication().getContent().size(), "验证返回内容空");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}